package com.walterweimod.auxiliary_attributes.util;

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.monster.ElderGuardian;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.plexus.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.Objects;

public class ModUtils {
    public static boolean isAlive(LivingEntity entity){
        return Objects.nonNull(entity) && entity.isAlive();
    }

    public static float getLivingEntityHealthPercent(LivingEntity entity){
        if (isAlive(entity)){
            return (entity.getHealth() / entity.getMaxHealth()) * 100.0f;
        }else {
            return Float.NaN;
        }
    }

    public static float getLivingEntityMaxHealthPercent(LivingEntity entity, float percent){
        if (isAlive(entity)){
            return entity.getMaxHealth() * (Math.max(percent, 0.0f) / 100.0f);
        }else {
            return Float.NaN;
        }
    }

    public static float getProduct(Collection<Float> numbers){
        float product = 1.0F;

        if (Objects.nonNull(numbers) && !numbers.isEmpty()){
            for (float number : numbers) {
                product *= number;
            }
        }

        return product;
    }

    public static String hyphenName(String name, int num){

        return "-".repeat(Math.max(0, num)) +
                (StringUtils.isNotEmpty(name) ? name : "") +
                "-".repeat(Math.max(0, num));
    }

    public static <T extends Comparable<T>> T clamp(T value, T min_value, T max_value) {
        if (value == null) {
            return null; // 直接返回 null，避免后续空指针异常
        }

        // 如果 min_value 不为空且 value 小于 min_value，返回 min_value
        if (min_value != null && value.compareTo(min_value) < 0) {
            return min_value;
        }

        // 如果 max_value 不为空且 value 大于 max_value，返回 max_value
        if (max_value != null && value.compareTo(max_value) > 0) {
            return max_value;
        }

        // 否则返回 value 本身
        return value;
    }

    public static Integer DoubleToInt(Double d){
        if (Objects.isNull(d)){
            return null;
        }else {
            return d >= 0.0D ? d.intValue() : d.intValue() - 1;
        }
    }
}
